package com.ruoyi.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.TPatientInfo;
import com.ruoyi.business.service.ITPatientInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.AppTableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TDailyPlan;
import com.ruoyi.business.service.ITDailyPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 每日计划Controller
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/business/plan")
public class TDailyPlanController extends BaseController
{
    @Autowired
    private ITDailyPlanService tDailyPlanService;


    @Autowired
    private ITPatientInfoService tPatientInfoService;
    /**
     * 查询每日计划列表
     */
    @PreAuthorize("@ss.hasPermi('business:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDailyPlan tDailyPlan)
    {
        startPage();
        List<TDailyPlan> list = tDailyPlanService.selectTDailyPlanList(tDailyPlan);
        return getDataTable(list);
    }

    @GetMapping("/listByApp")
    public AppTableDataInfo listByApp(TDailyPlan tDailyPlan)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String phone = user.getPhonenumber();
        TPatientInfo tPatientInfo = new TPatientInfo();
        tPatientInfo.setPatientPhone(phone);
        List<TPatientInfo> patientList = tPatientInfoService.selectTPatientInfoList(tPatientInfo);
        TPatientInfo patientInfo = patientList.get(0);
        tDailyPlan.setPatientId(patientInfo.getPatientId());
        tDailyPlan.setApp("1");
        TDailyPlan one = tDailyPlanService.selectTDailyPlanByPatientId(tDailyPlan);
        List<TDailyPlan> list = new ArrayList<>();
        list.add(one);
        return getAppDataTable(list);
    }


    /**
     * 导出每日计划列表
     */
    @PreAuthorize("@ss.hasPermi('business:plan:export')")
    @Log(title = "每日计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDailyPlan tDailyPlan)
    {
        List<TDailyPlan> list = tDailyPlanService.selectTDailyPlanList(tDailyPlan);
        ExcelUtil<TDailyPlan> util = new ExcelUtil<TDailyPlan>(TDailyPlan.class);
        util.exportExcel(response, list, "每日计划数据");
    }

    /**
     * 获取每日计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tDailyPlanService.selectTDailyPlanById(id));
    }
    /**
     * 获取每日计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:plan:query')")
    @GetMapping(value = "/getPlanByPatientId/{id}")
    public AjaxResult getPlanByPatientId(@PathVariable("id") String id)
    {
        TDailyPlan tDailyPlan = new TDailyPlan();
        tDailyPlan.setPatientId(id);
        return success(tDailyPlanService.selectTDailyPlanByPatientId(tDailyPlan)==null?tDailyPlan:tDailyPlanService.selectTDailyPlanByPatientId(tDailyPlan));
    }


    /**
     * 新增每日计划
     */
    @PreAuthorize("@ss.hasPermi('business:plan:add')")
    @Log(title = "每日计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDailyPlan tDailyPlan)
    {
        tDailyPlan.setId(UUID.randomUUID().toString());
        return toAjax(tDailyPlanService.insertTDailyPlan(tDailyPlan));
    }

    /**
     * 修改每日计划
     */
    @PreAuthorize("@ss.hasPermi('business:plan:edit')")
    @Log(title = "每日计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDailyPlan tDailyPlan)
    {
        return toAjax(tDailyPlanService.updateTDailyPlan(tDailyPlan));
    }

    /**
     * 删除每日计划
     */
    @PreAuthorize("@ss.hasPermi('business:plan:remove')")
    @Log(title = "每日计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tDailyPlanService.deleteTDailyPlanByIds(ids));
    }
}
